package com.example.mobile203zlw20216061300622;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttp;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private String AppCode="cd7de5dcbff64341905f0c0833ae5273";
    private String Simplified_forecast="http://aliv13.data.moji.com/whapi/json/alicityweather/briefforecast6days";
    private String Xxinshuju="http://aliv13.data.moji.com/whapi/json/aliweather/limit";
    private String tqzszliang="http://aliv13.data.moji.com/whapi/json/alicityweather/aqi";
    private String tqyujin="http://aliv13.data.moji.com/whapi/json/alicityweather/alert";
    private String tqskuang="http://aliv13.data.moji.com/whapi/json/alicityweather/condition";
    private String  token_Simplified_forecast="073854b56a84f8a4956ba3e273f6c9d7";
    private String token_Xxinshuju="27200005b3475f8b0e26428f9bfb13e9";
    private String token_tqzszliang="8b36edf8e3444047812be3a59d27bab9";
    private String token_tqyujin="7ebe966ee2e04bbd8cdbc0b84f7f3bc7";
    private String token_tqskuang="50b53ff8dd7d9fa320d3d3ca32cf8ed1";
    private Button buZhilian;
    private Button buJinjian;
    private Button buTianqiyubao;
    private Button buTqzkuang;
    private Button buXxinhshu;
    private TextView teXian;
    private TextView textView2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        OkHttpClient okHttp=new OkHttpClient();
        buZhilian = (Button) findViewById(R.id.bu_zhilian);
        buJinjian = (Button) findViewById(R.id.bu_jinjian);
        buTianqiyubao = (Button) findViewById(R.id.bu_tianqiyubao);
        buTqzkuang = (Button) findViewById(R.id.bu_tqzkuang);
        buXxinhshu = (Button) findViewById(R.id.bu_xxinhshu);
        teXian = (TextView) findViewById(R.id.te_xian);
        textView2 = (TextView) findViewById(R.id.textView2);
        buTqzkuang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        RequestBody requestBody=new FormBody.Builder().add("cityId","971")
                                .add("token",token_tqskuang).build();
                        Request request=new Request.Builder().addHeader("Authorization","APPCODE"+AppCode).url(tqskuang)
                                .post(requestBody).build();
                        Call call=okHttp.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String result=response.body().string();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        teXian.setText(result);
                                        Log.i("TAG","run"+result);
                                    }
                                });
                            }
                        });
                    }
                }).start();
            }
        });
    }
}